package wysiwyg.shapes
{
	
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	
	import wysiwyg.Options.OptionsBase;
	import wysiwyg.interfaces.Selectable;
	import wysiwyg.layers.Layer;
	import wysiwyg.model.Model;
	import wysiwyg.model.ModelLocator;
	import wysiwyg.objectHandles.HandleBase;

	public class ShapeBase extends UIComponent implements Selectable
	{
		protected var editingSubObject:Boolean;
		protected var mouseDownPos:Point;
		protected var origShapePos:Point;
		protected var isHoldingShape:Boolean;
		protected var theModel:Model=ModelLocator.getInstance().theModel;
		[Bindable]
		public var selected:Boolean=false;
		[Bindable]
		public var color:uint = 0xFFCCCC;
		[Bindable]
		public var gradientColor:Number;
		[Bindable]
		public var fillAlpha:Number=1;
		[Bindable]
		public var lineAlpha:Number=1;
		[Bindable]
		public var lineColor:uint=0x000000;
		[Bindable]
		public var lineThickness:Number=0;
		public var shapeOptions:OptionsBase;
		public var shapeHandles:HandleBase;
		public var parentLayer:Layer;
		private var clickedToDeselect:Boolean;
		
		
		public function ShapeBase()
		{
			super();
			addEventListener(MouseEvent.CLICK,clickedShape);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		protected function onMouseDown(event:MouseEvent):void
		{
			if(selected&&!editingSubObject)
			{
				isHoldingShape=true;
				
				origShapePos=new Point();
				mouseDownPos=new Point();
				
				origShapePos.x=shapeHandles.xToUse;
				origShapePos.y=shapeHandles.yToUse;
				
				mouseDownPos.x=theModel.workspace.mouseX;
				mouseDownPos.y=theModel.workspace.mouseY;
				
				theModel.workspace.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
				theModel.workspace.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			}
		}
		protected function onMouseUp(event:MouseEvent):void
		{
			if(Point.distance(mouseDownPos,new Point(theModel.workspace.mouseX,theModel.workspace.mouseY))==0)
			{
				deselect();
				clickedToDeselect=true;
			}
			isHoldingShape=false;
		}
		protected function onMouseMove(event:MouseEvent):void
		{
			if(isHoldingShape)
			{
				shapeHandles.xToUse=origShapePos.x+(theModel.workspace.mouseX-mouseDownPos.x);
				shapeHandles.yToUse=origShapePos.y+(theModel.workspace.mouseY)-mouseDownPos.y;
				shapeHandles.positionToCurrentShape();
			}
		}
		private function clickedShape(event:MouseEvent):void
		{
			if(!clickedToDeselect)
				select();
			clickedToDeselect=false;
		}
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			graphics.lineStyle(lineThickness,lineColor,lineAlpha);
			if(!isNaN(gradientColor))
			{
				var matrix:Matrix=new Matrix();
				matrix.createGradientBox(w,h);
				graphics.beginGradientFill(GradientType.LINEAR,[color,gradientColor],[fillAlpha,fillAlpha],[0,255],matrix);
			}
			else
				graphics.beginFill(color,fillAlpha);
		}
		public function select():void
		{
			if(selected)
				return;
			selected=true;
			dispatchEvent(new Event("selectedShape",true));
		}
		public function deselect():void
		{
			selected=false;
			dispatchEvent(new Event("deselectedShape",true));
		}
	}
}